﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using System.Collections;
using OgilvyOne.Model;
using ECCIT;
using ECCIT.Utilities;
using System.Xml;

public partial class GateService_AjaxHttpGate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;

        try
        {
            string cmd = Request.Params["cmd"];
            string sLangID = Request.Params["langID"];
            int langID = (sLangID == null || sLangID.Trim() == string.Empty) ? 1033 : int.Parse(sLangID);
            PagingList pList = null;
            ResultInfo res = null;
            string output = null;
            AccountController acc = new AccountController(langID);
            switch (cmd)
            {
               #region 网站首页-今日焦点
                
                case "hotspot": //网站首页-今日焦点
                    int listRows = int.Parse(Request.Params["rows"]); //今日焦点行数
                    IList list = new EventsBLL(langID).GetHotspot(listRows);
                    output = JsonUtil.Serialize(list);
                    break;
                #endregion

               #region 新闻列表
                case "newsPaging"://新闻列表
                    string sPagingIndex = Request.QueryString["currentPagingIndex"];
                    int currentPagingIndex = (sPagingIndex == null || sPagingIndex.Trim() == string.Empty) ? 0 : int.Parse(sPagingIndex);//当前页
                    int pagingSize = int.Parse(Request.QueryString["pagingsize"]);//每页显示个数
                    pList = new NewsBLL(langID).GetPagingNewsModel(currentPagingIndex, pagingSize);
                    output = JsonUtil.Serialize(pList);
                    break;
               #endregion

               #region 我的评论和上传
                case "myAccountPaging"://我的评论和上传
                    string sPageIndex = Request.QueryString["currentPagingIndex"];
                    int pageIndex = (sPageIndex == null || sPageIndex.Trim() == string.Empty) ? 0 : int.Parse(sPageIndex);//当前页
                    int pageSize = int.Parse(Request.QueryString["pagingsize"]);//每页显示个数
                    int? memberID = int.Parse(Request.QueryString["memberID"]);//用户ID
                    pList = new EventsCommentBLL(langID).GetEventCommentsByMemberID(memberID, pageIndex, pageSize);
                    output = JsonUtil.Serialize(pList);
                    break;
               #endregion

               #region 我的评论和上传--删除评论
                case "delEventComment"://我的评论和上传--删除评论
                    int eventCommentID = int.Parse(Request.QueryString["eventCommentID"]);//评论ID
                    res = new EventsCommentBLL(langID).delComment(eventCommentID);
                    output = JsonUtil.Serialize(res);
                    break;
               #endregion

               #region 活动评论
                case "eventComments"://活动评论
                    int eventId = int.Parse(Request.QueryString["eventID"]);//活动id
                    string fPageIndex = Request.QueryString["pageIndex"];
                    int cpageIndex = (fPageIndex == null || fPageIndex.Trim() == string.Empty) ? 0 : int.Parse(fPageIndex);//当前页
                    int cpageSize = int.Parse(Request.QueryString["pageSize"]);
                    PagingParameter paginParams = PagingParameterFactory.CreateInstance(true, cpageIndex, cpageSize, null);
                    pList = new EventsCommentBLL(langID).getEventsCommentPage(eventId, paginParams);
                    output = JsonUtil.Serialize(pList);
                    break;
               #endregion

               #region 保存活动评论数据
                case "SaveComments": //保存活动评论数据
                    int eID = int.Parse(Request.Params["eventID"]);
                    EventsCommentBLL ebll = new EventsCommentBLL(langID);
                    XmlDocument doc = new XmlDocument();
                    doc.Load(Request.InputStream);
                    string edescr = doc.GetElementsByTagName("desc")[0].InnerText;
                    string videoName = doc.GetElementsByTagName("videoName")[0].InnerText;
                    string photoName = doc.GetElementsByTagName("photoName")[0].InnerText;
                    string videoUrl = doc.GetElementsByTagName("videoUrl")[0].InnerText;
                    if (acc.AccountInfo == null)
                    {
                        res = new ResultInfo(false);
                        res.Message = "当前操作用户未登录";

                    }
                    else
                    {
                        EventsCommentModel ecModel = new EventsCommentModel();

                        ecModel.LanguageID = langID;
                        ecModel.EventsID = eID;
                        ecModel.Content = edescr;
                        ecModel.Time = DateTime.Now;
                        ecModel.MemberID = acc.AccountInfo.ID;
                        ecModel.VideoName = videoName;
                        ecModel.ImageName = photoName;
                        //ecModel.VideoUrl = videoUrl;
                        res = ebll.Save(ecModel);

                        //调用关键字日志接口
                        int? evid = ((EventsCommentModel)res.ReturnObject).ID;
                        LogMapKeyWordsBLL MkwBll = new LogMapKeyWordsBLL(langID);
                        Dictionary<MapKeyWordsModel, int> dmw = MkwBll.seleRepetKeyWordsModelList(edescr);
                        new LogMapKeyWordsBLL(langID).InsertLog(dmw, Convert.ToInt32(evid), 0);
                    }
                    output = JsonUtil.Serialize(res);
                    break;
               #endregion

               #region 回复评论
                case "replyComments":  //回复评论
                    int eventID = int.Parse(Request.Params["eventID"]);
                    int commentID = int.Parse(Request.Params["commentID"]);
                    EventsCommentBLL eCbll = new EventsCommentBLL(langID);
                    XmlDocument Xdoc = new XmlDocument();
                    Xdoc.Load(Request.InputStream);
                    string strInner = Xdoc.GetElementsByTagName("desc")[0].InnerText;

                    if (acc.AccountInfo == null)
                    {
                        res = new ResultInfo(false);
                        res.Message = "当前操作用户未登录";
                    }
                    else
                    {
                        EventsCommentModel ecModel = new EventsCommentModel();

                        ecModel.LanguageID = langID;
                        ecModel.EventsID = eventID;
                        ecModel.Content = strInner;
                        ecModel.Time = DateTime.Now;
                        ecModel.MemberID = acc.AccountInfo.ID;
                        ecModel.SourceMemberID = commentID;
                        res = eCbll.Save(ecModel);
                        output = JsonUtil.Serialize(res);
                    }
                    break;
                #endregion
               #region 相关活动
                case "getCorrelation":
                    int? evenId = int.Parse(Request.Params["eventID"]);
                    EventsBLL EventsBLL = new EventsBLL(langID);
                    IList<EventsModel> pl = EventsBLL.GetRelatedEvents(evenId, null);
                    output = JsonUtil.Serialize(pl);
                    break;
               #endregion
               #region 获取特色活动（分页）
                case "ReqFeatured":
                    int pgIndex = string.IsNullOrEmpty(Request.QueryString["pageIndex"]) == true ? 0 :int.Parse(Request.QueryString["pageIndex"]);
                    int pgSize = string.IsNullOrEmpty(Request.QueryString["pageSize"]) == true ? 0 : int.Parse(Request.QueryString["pageSize"]);
                    PagingParameter pparams = PagingParameterFactory.CreateInstance(true, pgIndex, pgSize, null);
                    pList = new EventsBLL(langID).GetFeaturedPaging(pparams);
                    output = JsonUtil.Serialize(pList);
                    break;
               #endregion
               #region 忘记密码-验证邮箱是否存在
                case "existsEmail":
                    string strEmail = Request.QueryString["strEmail"];
                    string flag = new MemberBLL(2052).CheckEmail(strEmail).Count.ToString();
                    output = flag;
                    break;
               #endregion
               #region 忘记密码-发送邮件
                case "sendEmail":
                    string email = Request.QueryString["email"];
                    IList<MemberModel> lists = new MemberBLL(2052).CheckEmail(email);
                    if (lists.Count != 0)
                    {
                        foreach (MemberModel model in lists)
                        {
                            model.ForgetPwrdM = Guid.NewGuid().ToString();
                            res = new MemberBLL(2052).Save(model);
                            if (res.Result)
                            {
                                string domainName = System.Configuration.ConfigurationManager.AppSettings["domain"].ToString();
                                string emailBody = langID == 2052 ? "<a href=\"" + domainName + "/CnUpdatePwd.aspx?Guid=" + model.ForgetPwrdM + "\">点击此连接重置新密码!</a>" : "<a href=\"" + domainName + "/UpdatePwd.aspx?Guid=" + model.ForgetPwrdM + "\">点击此连接重置新密码!</a>";
                                if (sendEmail(email, model.Password, emailBody))
                                {
                                    output = "1";
                                }
                                else
                                {
                                    output = "0";
                                }
                            }
                            else
                            {
                                output = "0";
                            }
                        }
                    }
                    else {
                        output = "0";
                    }
                    break;
               #endregion
               #region 查找该活动是否已经加入了收藏
                case "MyCalendar":
                    int evID = int.Parse(Request.QueryString["eventsID"]);
                    int membID = int.Parse(Request.QueryString["memberID"]);
                    EventCalendarModel modelInfo = new EventCalendarModel(membID, evID, langID);
                    if (new EventCalendarBLL(langID).IsExist(modelInfo))
                    {
                        output = "1";
                    }
                    else
                    {
                        output = "0";
                    }
                    break;
                #endregion
                #region 将活动加入收藏
                case "SetMyCalendar":
                    int eveID = int.Parse(Request.QueryString["eventsID"]);
                    int meID = int.Parse(Request.QueryString["memberID"]);
                    EventCalendarModel mInfo = new EventCalendarModel(meID, eveID, langID);
                    res = new EventCalendarBLL(langID).Save(mInfo);
                    output = JsonUtil.Serialize(res);
                    break;
                #endregion
                #region 获得我设置的提醒活动
                case "GetMyevent":
                    int memberId = int.Parse(Request.QueryString["memID"]);
                    FavoriteEventsModelExt fModel = new EventsBLL(langID).GetMyRecentlyEvent(memberId);
                    output = JsonUtil.Serialize(fModel);
                    break;
                #endregion
                #region 根据视频名称获取 视屏的转换状态
                case "CommentVideoStatus":
                    string commentVideoName = Request.QueryString["videoName"];
                    ConvertVideoBLL cvBll = new ConvertVideoBLL();
                    ConverVideoModel conVideoModel = cvBll.GetVideoStatusByOldName(commentVideoName);
                    output = JsonUtil.Serialize(conVideoModel);
                    break;
                #endregion
            }
            Response.Write(output);
        }
        catch (Exception ex)
        {
            Response.Write("<Error><![CDATA[Error occurred in XmlHttpGate. Phase: 1. Message: " + ex.Message + "]]></Error>");
        }
    }

    private bool sendEmail(string toMail, string pwd, string emailBody)
    {
        return MailService.sendMail(toMail, emailBody, "UKnow 艺述英国");
    }

}
